package com.example.demo213.controller;

import com.example.demo213.model.Student;
import com.example.demo213.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController


public class UserController {
    @Autowired
    UserService userService;
    @GetMapping("/students")
    public List<Student> getAllUser(){
        return userService.list();
    }
    @GetMapping("/student/{id}")
    public Student getStudentById (@PathVariable("id") Integer id){
        Student student = userService.getById(id);
        return student;
    }

    @PostMapping("/student")
    public String addStudent(@RequestBody Student student){
        if(userService.save(student)){
            return "添加成功！";
        }else{
            return "添加失败！";
        }
    }
    @PutMapping("/student")
    public String updateStudent(@RequestBody  Student student){
        if(userService.updateById(student)){
            return "修改成功！";
        }else {
            return "修改失败！";
        }
    }

    @DeleteMapping("/student")
    public String deleteStudent(Student student){
        if (userService.removeById(student)){
            return "删除成功！";
        }else {
            return "删除失败！";
        }
    }

}
